home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / Dump.c < prev    next >
Encoding:
Text File  |  1994-09-01  |  10.9 KB  |  437 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "CursorBalloon.h"
  14. #include    "Disasm.h"
  15. #include    "Dump.h"
  16. #include    "DoEditDialog.h"
  17. #include    "DoMenu.h"
  18. #include    "Globals.h"
  19. #include    "Main.h"
  20. #include    "Input.h"
  21. #include    "Registers.h"
  22. #include    "Scroll.h"
  23. #include    "TrackThumb.h"
  24. #include    "Conversions.h"
  25.  
  26. #if defined(FabSystem7orlater)
  27.  
  28. //#pragma segment Main
  29.  
  30. ControlHandle    dumpVScroll;
  31. short    dumpLineHeight, dumpFromTop, dumpCWidMax;
  32. static WindowPtr    curPosW;
  33.  
  34. static void NoRealTimeActionProc(ControlHandle control, short value);
  35. static void RealTimeActionProc(ControlHandle control, short value);
  36. static pascal void DumpActionProc(ControlHandle control, short part);
  37. static void DrawLine(Ptr start);
  38. static short TrackDumpObject(RectPtr r, short theLoc);
  39.  
  40. void DumpHome(void)
  41. {
  42. SetCtlValue(dumpVScroll, GetCtlMin(dumpVScroll));
  43. InvalDump();
  44. }
  45.  
  46. void DumpEnd(void)
  47. {
  48. SetCtlValue(dumpVScroll, GetCtlMax(dumpVScroll));
  49. InvalDump();
  50. }
  51.  
  52. void DumpPgUp(void)
  53. {
  54. DumpActionProc(dumpVScroll, inPageUp);
  55. }
  56.  
  57. void DumpPgDn(void)
  58. {
  59. DumpActionProc(dumpVScroll, inPageDown);
  60. }
  61.  
  62. void Activate_Dump(WindowPtr w, Boolean becomingActive)
  63. {
  64. Rect    growRect;
  65.  
  66. /* the growbox needs to be redrawn on activation: */
  67. growRect = w->portRect;
  68. /* adjust for the scrollbars */
  69. growRect.top = growRect.bottom - kScrollbarAdjust + 1;
  70. growRect.left = growRect.right - kScrollbarAdjust + 1;
  71. if (becomingActive) {
  72.     InvalRect(&growRect);    /* we cannot avoid grow box flicker */
  73.     if ((*dumpVScroll)->contrlVis == 0) {
  74.         ShowControl(dumpVScroll);
  75.         ValidRect(&(*dumpVScroll)->contrlRect);
  76.         }
  77.     }
  78. else {
  79. /* the control must be redrawn on deactivation: */
  80.     HideControl(dumpVScroll);
  81.     InvalRect(&growRect);
  82.     }
  83. } /*Activate*/
  84.  
  85. void Grow_Dump(WindowPtr w, EventRecord *event)
  86. {
  87. Rect    tempRect, updateRect;
  88. register long    growResult;
  89.  
  90. tempRect.right = tempRect.left = PRCT_R(w) + 1;
  91. tempRect.bottom = 0x7FFF;        /* set up limiting values */
  92. tempRect.top = kMinDocDim + 10;
  93. updateRect = w->portRect;
  94. updateRect.top = updateRect.bottom - kScrollbarAdjust;
  95. /* see if it really changed size */
  96. if (growResult = GrowWindow(w, event->where, &tempRect)) {
  97.     SizeWindow(w, LoWrd(growResult),
  98.                 (HiWrd(growResult) / dumpLineHeight) * dumpLineHeight, true);
  99.     SizeControl(dumpVScroll, kScrollbarWidth, PRCT_B(w) - PRCT_T(w) - 13);
  100.     if (HiWrd(growResult) > updateRect.bottom)
  101.         /* enlarged */
  102.         InvalRect(&updateRect);
  103.     else /* reduced */ {
  104.         updateRect.bottom = PRCT_B(w);
  105.         updateRect.right = PRCT_R(w);
  106.         updateRect.top = updateRect.bottom - kScrollbarAdjust + 1;
  107.         updateRect.left = updateRect.right - kScrollbarAdjust + 1;
  108.         InvalRect(&updateRect);
  109.         }
  110.     ValidRect(&(*dumpVScroll)->contrlRect);
  111.     SetupDumpCtlMax(dumpVScroll);
  112.     }
  113. }
  114.  
  115. void Update_Dump(WindowPtr w)
  116. {
  117. Rect    growRect;
  118. register RgnHandle    oldClip;
  119.  
  120. if (EmptyRgn(w->visRgn) == false) {    /* draw if updating needs to be done */
  121.     DrawDump(w);
  122.     oldClip = NewRgn();
  123.     GetClip(oldClip);
  124.     growRect = w->portRect;
  125.     growRect.left = growRect.right - kScrollbarAdjust;
  126.     ClipRect(&growRect);
  127.     DrawGrowIcon(w);
  128.     SetClip(oldClip);
  129.     DisposeRgn(oldClip);
  130.     UpdtControl(w, w->visRgn);
  131.     }
  132. }
  133.  
  134. void Do_Dump(WindowPtr w, EventRecord *event)
  135. {
  136. enum {
  137. kCurPosWindow = 128
  138. };
  139.  
  140. Rect    tempRect;
  141. GrafPtr    savePort;
  142. ControlHandle    control;
  143. Point    mouse;
  144. register unsigned long    clickAddr;
  145. register short    part, offset, popItem;
  146.  
  147. mouse = event->where;        /* get the click position */
  148. GlobalToLocal(&mouse);
  149. /* see if we are in the dump area; if so, we won’t check the controls */
  150. tempRect = w->portRect;
  151. tempRect.right -= kScrollbarAdjust;
  152. if (PtInRect(mouse, &tempRect)) {
  153.     /* handle editing click */
  154.     if ((offset = mouse.h - dumpCWidMax * 6 - kDIST_FROMLEFT) >= 0)
  155.         if ((offset /= dumpCWidMax) % 5 != 4) {
  156.             tempRect.top = (mouse.v /= dumpLineHeight) * dumpLineHeight;
  157.             tempRect.left = ((offset / 5) * 5) * dumpCWidMax + dumpCWidMax * 6 + kDIST_FROMLEFT;
  158.             tempRect.bottom = tempRect.top + dumpLineHeight;
  159.             tempRect.right = tempRect.left + (dumpCWidMax << 2);
  160.             clickAddr = ((unsigned long)(GetCtlValue(dumpVScroll) + mouse.v) << 4)
  161.                         + ((offset / 5) << 1);
  162.             if (popItem = TrackDumpObject(&tempRect, *(unsigned short *)(gMMemory + clickAddr))) {
  163.                 if (popItem == kD_Disasm) {
  164.                     SetCtlValue(disasmVScroll, clickAddr >> 2);
  165.                     InvalDisasm();
  166.                     DoMenuWindows(kMItem_Disasm);
  167.                     }
  168.                 else if ((event->modifiers & optionKey)||(popItem == kD_DisasmFrom)) {
  169.                     SetCtlValue(disasmVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 1);
  170.                     InvalDisasm();
  171.                     DoMenuWindows(kMItem_Disasm);
  172.                     }
  173.                 else if ((event->modifiers & cmdKey)||(popItem == kD_DumpFrom)) {
  174.                     SetCtlValue(dumpVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 3);
  175.                     InvalDump();
  176.                     }
  177.                 else {
  178.                     if (DoEditDump((short *)(gMMemory + clickAddr), clickAddr >> 1)) {
  179.                         InvalDump();
  180.                         InvalDisasm();
  181.                         }
  182.                     UnloadSeg(DoEditDump);
  183.                     }
  184.                 }
  185.             }
  186.     }
  187. else {
  188.     part = FindControl(mouse, w, &control);
  189.     switch ( part ) {
  190.         case 0:        /* do nothing for viewRect case */
  191.             break;
  192.         case inThumb:
  193.             if (gPrefs.NeXTScroll)
  194.                 (void)TrackThumb(control, mouse, RealTimeActionProc);
  195.             else {
  196.                 GetPort(&savePort);
  197.                 SetPort(curPosW = GetNewWindow(kCurPosWindow, nil, (WindowPtr)-1L));
  198.                 TextMode(srcCopy);
  199.                 TextFont(monaco);
  200.                 TextSize(9);
  201.                 SetPort(savePort);
  202.                 savePort = (GrafPtr)LMGetGhostWindow();
  203.                 LMSetGhostWindow((WindowPeek)curPosW);
  204.                 ShowWindow(curPosW);
  205.                 (void)TrackThumb(control, mouse, NoRealTimeActionProc);
  206.                 DisposeWindow(curPosW);
  207.                 LMSetGhostWindow((WindowPeek)savePort);
  208.                 InvalDump();
  209.                 }
  210.             break;
  211.         default:    /* clicked in an arrow, so track & scroll */
  212.             {
  213.             ControlActionUPP DumpActionProcUPP = NewControlActionProc(DumpActionProc);
  214.  
  215.             (void)TrackControl(control, mouse, DumpActionProcUPP);
  216.             if (DumpActionProcUPP)
  217.                 DisposeRoutineDescriptor(DumpActionProcUPP);
  218.             }
  219.             break;
  220.         }
  221.     }
  222. }
  223.  
  224. /* NoRealTimeActionProc: only updates the number in the little window up there */
  225.  
  226. static void NoRealTimeActionProc(ControlHandle , short value)
  227. {
  228. Str15    tempS;
  229. GrafPtr    savePort;
  230.  
  231. ShortToHexString(value << 3, tempS);
  232. GetPort(&savePort);
  233. SetPort(curPosW);
  234. MoveTo(kDIST_FROMLEFT,12);
  235. DrawString(tempS);
  236. SetPort(savePort);
  237. }
  238.  
  239. /* RealTimeActionProc: updates all of the window contents while dragging the scroll box */
  240.  
  241. static void RealTimeActionProc(ControlHandle control, short )
  242. {
  243. DrawDump((*control)->contrlOwner);
  244. }
  245.  
  246. static pascal void DumpActionProc(ControlHandle control, short part)
  247. {
  248. Rect    tempRect;
  249. register WindowPtr    w;
  250. register Ptr    addr;
  251. register short    amount, oldAmount, newAmount, vc;
  252. register Boolean    doScrollRect = false;
  253.  
  254. if ( part ) {        /* if it was actually in the control */
  255.     w = (*control)->contrlOwner;
  256.     switch ( part ) {
  257.         case inUpButton:
  258.             amount = -1;
  259.             doScrollRect = true;
  260.             break;
  261.         case inDownButton:
  262.             amount = 1;
  263.             doScrollRect = true;
  264.             break;
  265.         case inPageUp:
  266.             amount = (PRCT_T(w) - PRCT_B(w)) / dumpLineHeight + 1;
  267.             break;
  268.         case inPageDown:
  269.             amount = (PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1;
  270.             break;
  271.         }
  272.     SetCtlValue(control, (oldAmount = GetCtlValue(control)) + amount);
  273.     if (doScrollRect && (newAmount = oldAmount - GetCtlValue(control))) {
  274.         tempRect = w->portRect;
  275.         tempRect.right -= kScrollbarAdjust;
  276.         VScrollRect(&tempRect, newAmount * dumpLineHeight);
  277.         addr = &gMMemory[(unsigned long)GetCtlValue(control) << 4];
  278.         vc = dumpFromTop;
  279.         if (newAmount < 0) {
  280.             vc += PRCT_B(w) - PRCT_T(w) - dumpLineHeight;
  281.             addr += ((PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1) << 4;
  282.             }
  283.         MoveTo(PRCT_L(w) + kDIST_FROMLEFT, vc);
  284.         DrawLine(addr);
  285.         }
  286.     else
  287.         DrawDump(w);
  288.     }
  289. } /* DumpActionProc */
  290.  
  291. void DrawDump(WindowPtr w)
  292. {
  293. Rect    tempRect;
  294. GrafPtr    savePort;
  295. Point    tempPoint;
  296. register Ptr    addr;
  297. register short    j;
  298.  
  299. GetPort(&savePort);
  300. SetPort(w);
  301. tempRect = w->portRect;
  302. tempRect.bottom += dumpLineHeight;
  303. addr = &gMMemory[(unsigned long)GetCtlValue(dumpVScroll) << 4];
  304. for(tempPoint.h = kDIST_FROMLEFT, j = dumpFromTop;
  305.     tempPoint.v = j, PtInRect(tempPoint, &tempRect);
  306.     j += dumpLineHeight, addr += 16) {
  307.  
  308.     MoveTo(PRCT_L(w) + kDIST_FROMLEFT, j);
  309.     DrawLine(addr);
  310.     }
  311. SetPort(savePort);
  312. }
  313.  
  314. /* DrawLine: draws a "line" of memory in the Dump window */
  315.  
  316. static void DrawLine(Ptr start)
  317. {
  318. Str63    tempS;
  319. register short *hexPtr, *endPtr;
  320. register Ptr    textPtr;
  321.  
  322. endPtr = (short *)(start + 16);
  323. textPtr = ShortToHexText(PTR2MEMWORD(start), (Ptr)&tempS);
  324. *textPtr++ = ':';
  325. for (hexPtr = (short *)start; hexPtr < endPtr; ) {
  326.     *textPtr++ = ' ';
  327.     textPtr = ShortToHexText(*hexPtr++, textPtr);
  328.     }
  329. DrawText(&tempS, 0, 45);
  330. }
  331.  
  332. /* SetupDumpCtlMax: sets up the CtlMax value of our scroll bar */
  333.  
  334. void SetupDumpCtlMax(ControlHandle theControl)
  335. {
  336. enum {
  337. kAdjustForPleasantGrow = 3
  338. };
  339.  
  340. register WindowPtr    wind;
  341. register short    newmax;
  342.  
  343. wind = (*theControl)->contrlOwner;
  344. newmax = 8191 - (PRCT_B(wind) - PRCT_T(wind) - kAdjustForPleasantGrow) / dumpLineHeight;
  345. if (newmax != GetCtlMax(theControl)) {
  346.     SetCtlMax(theControl, newmax);
  347.     InvalDump();
  348.     }
  349. }
  350.  
  351. void InvalDump(void)
  352. {
  353. Rect    tempRect;
  354. GrafPtr    savePort;
  355.  
  356. GetPort(&savePort);
  357. SetPort(gWPtr_Dump);
  358. tempRect = gWPtr_Dump->portRect;
  359. tempRect.right -= kScrollbarWidth;
  360. InvalRect(&tempRect);
  361. SetPort(savePort);
  362. }
  363.  
  364. /* TrackDumpObject: like all the TrackThing in the Mac OS, plus the popUp */
  365.  
  366. short    TrackDumpObject(RectPtr    r, short theLocation)
  367. {
  368. enum {
  369. kSimpleClick = -1,
  370. kDelay = 1 * 60 // one second
  371. };
  372.  
  373. Str32    itemStr;
  374. Str15    numStr;
  375. Point    myPt;
  376. MenuHandle    tempMenu;
  377. register long        timeout = TickCount();
  378. register long        chosen = kSimpleClick;
  379. register Boolean    inrect;
  380.  
  381. InvertRect(r);
  382. inrect = true;
  383. do {
  384.     register Boolean tempB;
  385.  
  386.     GetMouse(&myPt);
  387.     if ((tempB = PtInRect(myPt, r)) != inrect) {
  388.         InvertRect(r);
  389.         inrect = tempB;
  390.         }
  391.     if (inrect == false)
  392.         timeout = TickCount();
  393.     if ((TickCount() - timeout) > GetDblTime()) {
  394.         tempMenu = gPopMenu;
  395.         if (noErr == HandToHand((Handle *)&tempMenu)) {
  396.             GetItem(tempMenu, kD_DisasmFrom, itemStr);
  397.             ShortToHexString(theLocation, numStr);
  398.             PLstrcat(itemStr, numStr);
  399.             SetItem(tempMenu, kD_DisasmFrom, itemStr);
  400.             GetItem(tempMenu, kD_DumpFrom, itemStr);
  401.             PLstrcat(itemStr, numStr);
  402.             SetItem(tempMenu, kD_DumpFrom, itemStr);
  403.             InsertMenu(tempMenu, hierMenu);
  404.             LocalToGlobal(&myPt);
  405.             chosen = PopUpMenuSelect(tempMenu, myPt.v, myPt.h, kD_Edit);
  406.             DeleteMenu(kRes_Menu_PopDump);
  407.             DisposeMenu(tempMenu);
  408.             }
  409.         }
  410.     }
  411. while( StillDown() );
  412. if (inrect)
  413.     InvertRect(r);
  414. if (chosen == kSimpleClick)
  415.     chosen = inrect ? toMenu(kRes_Menu_PopDump, kD_Edit) : 0L;
  416. return HiWrd(chosen) ? LoWrd(chosen) : 0;
  417. }
  418.  
  419. /* procedure called when closing the Dump window */
  420.  
  421. void CloseDump(WindowPtr w)
  422. {
  423. DoCloseWindow(w, kMItem_Dump);
  424. }
  425.  
  426. void RecalcDump(FabWindowPtr w, RgnBalloonCursPtr theObj)
  427. {
  428. Rect    tempRect;
  429.  
  430. tempRect = ((WindowPtr)w)->portRect;
  431. tempRect.right -= kScrollbarWidth;
  432. RectRgn(theObj->zoneLocal, &tempRect);
  433. }
  434.  
  435. #endif
  436.  
  437.